import { ToolHandlers } from './ToolHandlers.js';

export class CallToolHandler {
  private toolHandlers: ToolHandlers;

  constructor(toolHandlers: ToolHandlers) {
    this.toolHandlers = toolHandlers;
  }

  async handleToolCall(name: string, args: any): Promise<any> {
    switch (name) {
      case 'analyze_java_file':
        return await this.toolHandlers.analyzeJavaFile(args.filePath, args.rules);
      case 'analyze_springboot_project':
        return await this.toolHandlers.analyzeSpringBootProject(args.projectPath);
      case 'refactor_large_file':
        return await this.toolHandlers.refactorLargeFile(args.filePath, args.strategy);
      case 'refactor_large_method':
        return await this.toolHandlers.refactorLargeMethod(args.filePath, args.methodName, args.strategy);
      case 'optimize_java8_features':
        return await this.toolHandlers.optimizeJava8Features(args.filePath, args.features);
      case 'check_single_responsibility':
        return await this.toolHandlers.checkSingleResponsibility(args.filePath);
      case 'clean_unused_code':
        return await this.toolHandlers.cleanUnusedCode(args.projectPath, args.aggressive);
      case 'maven_compile':
        return await this.toolHandlers.mavenCompile(args.projectPath, args.clean);
      case 'maven_test':
        return await this.toolHandlers.mavenTest(args.projectPath, args.skipTests);
      case 'suggest_springboot_improvements':
        return await this.toolHandlers.suggestSpringBootImprovements(args.projectPath);
      case 'split_large_service':
        return await this.toolHandlers.splitLargeService(args.filePath, args.splitStrategy);
      case 'generate_service_interface':
        return await this.toolHandlers.generateServiceInterface(args.filePath, args.interfaceName, args.implName);
      case 'update_cross_file_references':
        return await this.toolHandlers.updateCrossFileReferences(args.targetFilePath, args.projectPath, args.methodChanges);
      case 'move_package':
        return await this.toolHandlers.movePackage(args.sourceFilePath, args.targetPackage, args.projectPath, args.updateReferences);
      case 'introduce_parameter_object':
        return await this.toolHandlers.introduceParameterObject(args.filePath, args.methodName, args.parameterObjectName, args.projectPath, args.minParameterCount);
      case 'encapsulate_fields':
        return await this.toolHandlers.encapsulateFields(args.filePath, args.projectPath, args.includeProtected, args.generateSetters);
      case 'refactor_to_strategy':
        return await this.toolHandlers.refactorToStrategy(args.filePath, args.methodName, args.strategyInterfaceName, args.projectPath);
      case 'extract_interface':
        return await this.toolHandlers.extractInterface(args.classFiles, args.interfaceName, args.projectPath, args.outputDir);
      case 'introduce_builder':
        return await this.toolHandlers.introduceBuilder(args.filePath, args.minParameterCount, args.builderClassName);
      case 'rename_class':
        return await this.toolHandlers.renameClass(args.filePath, args.oldClassName, args.newClassName, args.projectPath);
      case 'rename_method':
        return await this.toolHandlers.renameMethod(args.filePath, args.className, args.oldMethodName, args.newMethodName, args.projectPath);
      case 'extract_method':
        return await this.toolHandlers.extractMethod(args.filePath, args.startLine, args.endLine, args.newMethodName, args.returnType);
      case 'auto_refactor_large_method':
        // 🚨 完全禁用：直接在CallToolHandler层面阻止调用
        console.error(`[CallToolHandler] 🛑 完全禁用auto_refactor_large_method调用`);
        return {
          success: false,
          message: '🚨 **功能已完全禁用** - auto_refactor_large_method存在严重的代码破坏风险\n\n' +
                   '❌ **问题**：该功能会破坏Java代码结构，生成语法错误\n' +
                   '🛡️ **安全措施**：已在调用层面完全禁用此功能\n\n' +
                   '✅ **安全替代方案**：\n' +
                   '1. 使用 `analyze_java_file` 分析代码结构\n' +
                   '2. 使用 `extract_method` 手动提取特定代码块\n' +
                   '3. 每次只重构小的逻辑单元\n' +
                   '4. 提取后立即验证代码编译\n\n' +
                   '🔧 **推荐工作流程**：\n' +
                   '• 分析 → 手动提取 → 验证 → 重复',
          filesModified: [],
          extractedMethods: [],
          errors: ['功能已禁用：存在代码破坏风险'],
          validationResults: {
            syntaxValid: true, // 不修改文件所以安全
            compilationTested: false
          }
        };
      case 'extract_magic_values':
        return await this.toolHandlers.extractMagicValues(args.filePath, args.autoFix);
      case 'suggest_enums':
        return await this.toolHandlers.suggestEnums(args.filePath, args.autoCreate);
      case 'optimize_springboot_annotations':
        return await this.toolHandlers.optimizeSpringBootAnnotations(args.filePath);
        
      // ============ 前端重构工具处理 ============
      
      // React重构
      case 'convert_react_class_to_hooks':
        return await this.toolHandlers.convertReactClassToHooks(args.filePath);
      case 'extract_custom_hook':
        return await this.toolHandlers.extractCustomHook(args.filePath);
      case 'optimize_react_performance':
        return await this.toolHandlers.optimizeReactPerformance(args.filePath);
        
      // Vue重构
      case 'convert_vue_to_composition_api':
        return await this.toolHandlers.convertVueToCompositionAPI(args.filePath);
      case 'extract_vue_composables':
        return await this.toolHandlers.extractVueComposables(args.filePath);
      case 'optimize_vue_performance':
        return await this.toolHandlers.optimizeVuePerformance(args.filePath);
        
      // CSS重构
      case 'convert_css_to_bem':
        return await this.toolHandlers.convertCSSBEM(args.filePath);
      case 'convert_css_to_css_in_js':
        return await this.toolHandlers.convertCSSToCSSInJS(args.filePath);
      case 'optimize_scss':
        return await this.toolHandlers.optimizeSCSS(args.filePath);
      case 'optimize_css_performance':
        return await this.toolHandlers.optimizeCSSPerformance(args.filePath);
        
      // JavaScript现代化  
      case 'modernize_javascript_to_es6':
        return await this.toolHandlers.modernizeJavaScriptToES6(args.filePath);
      case 'convert_promise_to_async_await':
        return await this.toolHandlers.convertPromiseToAsyncAwait(args.filePath);
      case 'optimize_javascript_performance':
        return await this.toolHandlers.optimizeJavaScriptPerformance(args.filePath);
        
      // AI集成工具
      case 'ai_code_review':
        return await this.toolHandlers.aiCodeReview(args.filePath, args.aiProvider, args.reviewType);
      case 'ai_refactor_plan':
        return await this.toolHandlers.aiRefactorPlan(args.targetPath, args.planType, args.aiProvider);
        
      default:
        throw new Error(`未知的工具: ${name}`);
    }
  }
}